namespace LocalBoard.Web
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using LocalBoard.Controller;
	/// <summary>
	///		Summary description for BoardTopBar.
	/// </summary>
	public partial class BoardTopBar : BoardUserControl
	{
		protected BoardController boardManager
		{
			get{return ((BoardPage)Page).BoardController;}
		}
		protected string CurrentBoardPageUrl
		{
			get { return ((BoardPage)Page).CurrentBoardPageUrl; }
		}


		protected void Page_Load(object sender, System.EventArgs e)
		{
			newMessage.NavigateUrl = CurrentBoardPageUrl + "&mode=new";
			latestList.NavigateUrl = CurrentBoardPageUrl;
			if(boardManager.HasPreviousPage)
				prev.NavigateUrl = GetMessageListPageUrl(boardManager.CurrentPageNo - 1);
			if(boardManager.HasNextPage)
				next.NavigateUrl = GetMessageListPageUrl(boardManager.CurrentPageNo + 1);
			if(!boardManager.HasPreviousPage && !boardManager.HasNextPage)
			{
				pageList.Visible = false;
				totalPageNo.Visible = false;
			}

			for(int i=1;i<=boardManager.TotalPageNumber;i++)
			{
				pageList.Items.Add(new ListItem(i.ToString(), i.ToString()));
			}
            if (pageList.Items.Count > 0)
            {
                pageList.SelectedIndex = boardManager.CurrentPageNo - 1;
                totalPageNo.Text = string.Format(totalPageNo.Text, boardManager.TotalPageNumber.ToString());
            }
			if(!boardManager.GetBoardSecurity().CanUserPostMessage(this.ClubUser))
				newMessage.Visible = false;
		}

		private string GetMessageListPageUrl(int pageNo)
		{
			return ClubPage.AddPageQueryStringToUrl(CurrentBoardPageUrl, pageNo);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void pageList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Response.Redirect(GetMessageListPageUrl(Convert.ToInt32(pageList.SelectedValue)));
		}
	}
}
